package com.chhuang.system.controller;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chhuang.system.bean.MenuWithRoleIdQueryBean;
import com.chhuang.system.consts.SysConstants;
import com.chhuang.system.po.ChMenu;
import com.chhuang.system.po.ChRole;
import com.chhuang.system.po.ChRoleMenuMap;
import com.chhuang.system.service.IChMenuService;
import com.chhuang.system.service.IChRoleMenuMapService;
import com.chhuang.system.service.IChRoleService;
import com.chhuang.utils.Assert;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/system/roleMenuMap")
public class ChRoleMenuMapController {
	private static final Log log = LogFactory.getLog(ChRoleMenuMapController.class);
	
	@Autowired
	private IChRoleService chRoleService;
	@Autowired
	private IChMenuService chMenuService;
	@Autowired
	private IChRoleMenuMapService chRoleMenuMapService;
	
	/**
	 * 根据菜单id获取角色列表
	 * @param menuId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/getRolesByMenuId",method=RequestMethod.POST)
	@ResponseBody
	public Object getRolesBymenuId(String menuId) throws Exception{
		List<ChRole> roles = null;
		List<ChRoleMenuMap> roleMenuMaps = chRoleMenuMapService.findByMenuId(menuId);
		if(Assert.isValidCollection(roleMenuMaps)){
			String[] roleIds = new String[roleMenuMaps.size()];
			for(int i=0;i<roleMenuMaps.size();i++){
				roleIds[i] = roleMenuMaps.get(i).getRoleId();
			}
			roles = chRoleService.findByRoleIds(roleIds);
			return roles;
		}
		log.debug("根据菜单id获取角色列表");
		return SysConstants.Symbol.EMPTY_JSONARRAY;
	}
	
	/**
	 * 
	 * @param pageSize
	 * @param pageNum
	 * @param orderBy
	 * @param desc
	 * @param or
	 * @param searchText
	 * @param roleId
	 * @param isIn
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/getMenus",method=RequestMethod.POST)
	@ResponseBody
	public Object getMenus(@RequestBody MenuWithRoleIdQueryBean menuWRoleIdqueryBan) throws Exception{
		log.debug(menuWRoleIdqueryBan.getPageSize()+";"+
				menuWRoleIdqueryBan.getPageNum()+";"+
				menuWRoleIdqueryBan.getRoleId()+";"+
				menuWRoleIdqueryBan.getMenuName()+";"+
				menuWRoleIdqueryBan.getUrl()+";"+
				menuWRoleIdqueryBan.getOrderBy()+";"+
				menuWRoleIdqueryBan.isAsc());
		return chRoleMenuMapService.findMenusByRoleIdAndQueryConditions(menuWRoleIdqueryBan);
	}
	
	/**
	 * 给角色添加菜单
	 * @param roleId
	 * @param menuIds
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/roleAddMenus",method=RequestMethod.POST)
	@ResponseBody
	public Object roleAddMenus(String roleId,String menuIds) throws Exception{
		JSONObject json = new JSONObject();
		if(Assert.isValidString(roleId) && Assert.isValidString(menuIds)){
			ChRole role = chRoleService.findByPrimaryKey(roleId);
			if(role!=null){
				String[] menuIdArray = menuIds.split(SysConstants.Symbol.COMMA);
				for(String menuId : menuIdArray){
					ChMenu menu = chMenuService.findByPrimaryKey(menuId);
					if(menu!=null){
						ChRoleMenuMap roleMenuMap = new ChRoleMenuMap();
						roleMenuMap.setRoleId(roleId);
						roleMenuMap.setMenuId(menuId);
						roleMenuMap.setRoleOrder(role.getRoleOrder());
						roleMenuMap.setMenuOrder(menu.getMenuOrder());
						chRoleMenuMapService.save(roleMenuMap);
						json.put(SysConstants.Status.SUCCESS, true);
					}
				}
			}
		}
		return json;
	}
	
	/**
	 * 根据roleId和menuId删除角色与菜单的映射关系
	 * @param roleId
	 * @param menuId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/delRoleMenuMap",method=RequestMethod.POST)
	@ResponseBody
	public Object delRoleMenuMap(String roleId,String menuIds) throws Exception{
		JSONObject json = new JSONObject();
		if(Assert.isValidString(roleId) && Assert.isValidString(menuIds)){
			String[] menuIdArray = menuIds.split(SysConstants.Symbol.COMMA);
			for(String menuId : menuIdArray){
				chRoleMenuMapService.deleteByRoleIdMenuId(roleId, menuId);
			}
			json.put(SysConstants.Status.SUCCESS, true);
		}
		return json;
	}
}
